"""
Case Type   : invisible
Case Name   : 验证gs_dump工具导出SQL中包含invisible
Create At   : 2024/1/26
Owner       : zou_jialiang050
Description :
    1、创建B库并创建表及索引
    2、使用gs_dump备份数据
    3、查看备份文件
    4、清理环境
Expect      :
    1、成功
    2、成功
    3、备份文件中包含invisible索引字段
    4、成功
History     :
"""

import os
import unittest

from yat.test import Node
from yat.test import macro

from testcase.utils.Common import Common
from testcase.utils.CommonSH import CommonSH
from testcase.utils.Constant import Constant
from testcase.utils.Logger import Logger


class InvisibleTest(unittest.TestCase):

    def setUp(self):
        self.log = Logger()
        self.primary_sh = CommonSH('PrimaryDbUser')
        self.user_node = Node('PrimaryDbUser')
        self.constant = Constant()
        self.commonsh = CommonSH
        self.common = Common()
        self.db_name = 'db_invisible_0028'
        self.sc_name = 'sc_invisible_0028'
        self.t_name = 't_invisible_0028'
        self.index_name_1 = 'idx_invisible_0028_01'
        self.index_name_2 = 'idx_invisible_0028_02'
        self.index_name_3 = 'idx_invisible_0028_03'
        self.dump_file = 'invisible_0028_dump.sql'
        self.dump_path = os.path.join(macro.DB_INSTANCE_PATH, self.dump_file)
        self.log.info(f'-----{os.path.basename(__file__)} start-----')

    def test_invisible(self):
        text = '----step1:创建兼容b库;expect:成功----'
        self.log.info(text)
        sql_cmd = f"drop database if exists {self.db_name};" \
                  f"create database {self.db_name} " \
                  f"dbcompatibility ='B';"
        self.log.info(sql_cmd)
        sql_res = self.primary_sh.execut_db_sql(sql_cmd, dbname='postgres')
        self.log.info(sql_res)
        self.assertIn(self.constant.CREATE_DATABASE_SUCCESS, sql_res,
                      "执行失败" + text)

        text = '-----step2:创建表并创建索引，插入数据;expect:成功-----'
        self.log.info(text)
        sql_cmd = f"drop schema if exists {self.sc_name};" \
                  f"create schema {self.sc_name};" \
                  f"drop table if exists {self.sc_name}.{self.t_name};" \
                  f"create table {self.sc_name}.{self.t_name}" \
                  f"(col1 int,col2 varchar(50),col3 text,col4 numeric);" \
                  f"drop index if exists {self.sc_name}.{self.index_name_1};" \
                  f"create index {self.sc_name}.{self.index_name_1} on " \
                  f"{self.sc_name}.{self.t_name}(col1) invisible;" \
                  f"drop index if exists {self.sc_name}.{self.index_name_2};" \
                  f"create index {self.sc_name}.{self.index_name_2} on " \
                  f"{self.sc_name}.{self.t_name}(col2) visible;" \
                  f"drop index if exists {self.sc_name}.{self.index_name_3};" \
                  f"create index {self.sc_name}.{self.index_name_3} on " \
                  f"{self.sc_name}.{self.t_name}(col3,col4) invisible;"
        msg4 = self.primary_sh.execut_db_sql(sql_cmd, dbname=self.db_name)
        self.log.info(msg4)
        self.assertIn(self.constant.CREATE_TABLE_SUCCESS, msg4)
        self.assertEqual(3, msg4.count(self.constant.CREATE_INDEX_SUCCESS))

        text = '-----step3:gs_dump导出表;expect:成功-----'
        self.log.info(text)
        cmd = f'source {macro.DB_ENV_PATH};' \
              f'gs_dump ' \
              f'-p {self.user_node.db_port} {self.db_name} ' \
              f'-n {self.sc_name} -f {self.dump_path} '
        self.log.info(cmd)
        msg5 = self.common.get_sh_result(self.user_node, cmd)
        self.assertIn(f'dump database {self.db_name} successfully', msg5)
        cmd = f'cat {self.dump_path}'
        cat_res = self.common.get_sh_result(self.user_node, cmd)
        self.assertIn(self.index_name_1, cat_res, '索引dump失败')
        self.assertIn(self.index_name_2, cat_res, '索引dump失败')
        self.assertIn(self.index_name_3, cat_res, '索引dump失败')

    def tearDown(self):
        text = '-----step4:清理环境;expect:成功-----'
        self.log.info(text)
        sql_cmd = f"drop database if exists {self.db_name};"
        self.log.info(sql_cmd)
        sql_res = self.primary_sh.execut_db_sql(sql_cmd, dbname='postgres')
        self.log.info(sql_res)
        rm_res = self.common.get_sh_result(self.user_node,
                                           f'rm -rf {self.dump_path}')
        self.log.info(rm_res)
        self.assertIn(self.constant.DROP_DATABASE_SUCCESS, sql_res,
                      '执行失败' + text)
        self.assertEqual('', rm_res, '文件删除失败')
        self.log.info(f'-----{os.path.basename(__file__)} end-----')
